package mi.payment.admin.pay88;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.transaction.sale.code.CodeActivity;
import mi.payment.transaction.sale.code.ScanActivity;

public class IPay88PaySelectActivity extends AutoFinishActivity {

    private List<IPay88Wallet> mSourceData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_pay88_activity_pay_select);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        String amount = intent.getStringExtra("amount");
        boolean isMerchantScan = intent.getBooleanExtra("isMerchantScan", true);

        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.consume);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        RecyclerView recyclerView = findViewById(R.id.recycler_view);

        mSourceData = new ArrayList<>();
        HashMap<String, IPay88Wallet> map = IPay88Operation.getInstance().getIPay88WalletMap();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            IPay88Wallet wallet = map.get(key);
            if (isMerchantScan) {
                if (wallet.merchantSwitch) mSourceData.add(wallet);
            } else {
                if (wallet.userSwitch) mSourceData.add(wallet);
            }
        }

        PayAdapter adapter = new PayAdapter(this, mSourceData);
        GridLayoutManager layoutManage = new GridLayoutManager(this, 2);
        RecyclerItemDecoration itemDecoration = new RecyclerItemDecoration(32, 2);
        recyclerView.setLayoutManager(layoutManage);
        recyclerView.addItemDecoration(itemDecoration);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(
                (parent, view, position) -> {
                    IPay88Wallet item = mSourceData.get(position);
                    if (isMerchantScan) {
                        ScanActivity.startAction(mActivity, amount, item);
                    } else {
                        CodeActivity.startAction(mActivity, amount, item);
                    }
                }
        );
    }

    private class RecyclerItemDecoration extends RecyclerView.ItemDecoration {

        private int itemSpace;
        private int itemNum;

        RecyclerItemDecoration(int itemSpace, int itemNum) {
            this.itemSpace = itemSpace;
            this.itemNum = itemNum;
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.bottom = itemSpace;
            if (parent.getChildLayoutPosition(view) % itemNum == 0) {
                // parent.getChildLayoutPosition(view) 获取view的下标
                outRect.left = 0;
            } else {
                outRect.left = itemSpace;
            }
        }

    }

    public static void startAction(Context context, String amount, boolean isMerchantScan) {
        Intent intent = new Intent(context, IPay88PaySelectActivity.class);
        intent.putExtra("amount", amount);
        intent.putExtra("isMerchantScan", isMerchantScan);
        context.startActivity(intent);
    }

}
